package com.devicemantra.coordinate.dataobjects;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.devicemantra.coordinate.ds.tests.PMF;

@PersistenceCapable(identityType = IdentityType.DATASTORE, detachable =
"true")
public class FrameId
{
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long mKey;
	
	@Persistent(defaultFetchGroup="true")
    private long mSequence = 0;;
	
	public FrameId() 
	{
		mSequence = init();
	}
	
	public long init()
	{
		FrameId lFrameId = get();
		return lFrameId.mSequence;
	}
	
	public FrameId get()
	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		javax.jdo.Query query = pm.newQuery(FrameId.class);
		List<FrameId> lFrameIdList = (List<FrameId>) query.execute();
		/* There is only one object of this type , make it a singleton this design
		 * is wrong 
		 */
		FrameId lFrameId = lFrameIdList.get(0); 
		pm.close();
		return lFrameId;
	}
	
	
	
	public void save()
	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.makePersistent(this);
		pm.close();
		
	}
	
	public void increment()
	{
		mSequence++;
		save();
	}
}
